<template>
  <view>
    <view class="goods-item">
      <!-- 左侧的盒子 -->
      <view class="left-goods-item">
        <radio :checked="goods.goods_state" v-if="showRadio" color="#C00000" @click="change"></radio>
        <image :src="goods.goods_small_logo || defaultPic" mode=""></image>
      </view>
      <!-- 右侧的盒子 -->
      <view class="right-goods-item">
        <!-- 商品的名称 -->
        <view class="goods-name">
          {{goods.goods_name}}
        </view>
        <view class="goods-info-box">
          <!-- 商品的价格 -->
          <view class="goods-price">
            ￥{{goods.goods_price | toFixed}}
          </view>
          <uni-number-box  v-if="showNumber" :min="1" :value="goods.goods_count" @change="changeHandler()"></uni-number-box>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
      };
    },
    filters: {
      toFixed(value) {
        return Number(value).toFixed(2)
      }
    },
    props: {
      goods: {
        type: Object,
        default: {}
      },
      // 是否展示图片左侧的 radio
      showRadio: {
        type: Boolean,
        // 如果外界没有指定 show-radio 属性的值，则默认不展示 radio 组件
        default: false,
      },
      //是否展示数字框：
      showNumber:{
        type: Boolean,
        default: false
      }
    },
    methods: {
      //radio组件的点击事件：
      change() {
       this.$emit('radio',{
         //商品的id
         goods_id:  this.goods.goods_id,
         //商品最新的勾选状态：
         goods_state: !this.goods.goods_state
       })
      },
      //商品数量发生改变时，所触发的事件：
      changeHandler(val){
        this.$emit('numChange',{
          goods_id: this.goods.goods_id,
          //商品数量最新的值，+ 保证他是number类型
          goods_count: +val
        })
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    // 让 goods-item 项占满整个屏幕的宽度
    width: 750rpx;
    // 设置盒模型为 border-box
    box-sizing: border-box;
    padding: 10px 5px;
    display: flex;
    border-bottom: 1px solid #f0f0f0;

    .left-goods-item {
      margin-right: 5px;
      display: flex;
      align-items: center;

      image {
        width: 100px;
        height: 100px;
        display: block;
      }
    }

    .right-goods-item {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex: 1;

      .goods-name {
        font-size: 13px;
      }

      .goods-info-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .goods-price {
          font-size: 16px;
          color: #C00000;
        }
      }
    }
  }
</style>
